//
//  MSIncomeDetailsController.m
//  觅尚
//
//  Created by No.8 on 2019/4/1.
//  Copyright © 2019 No.8. All rights reserved.
//


#import "MSWithdrawalController.h"
#import "MSWithDrawalCell.h"
#import "MSWithdrawalModel.h"
#define pageSize 10

@interface MSWithdrawalController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
    NSInteger page;
    NSInteger count;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 累计提现*/
@property(nonatomic,strong)UILabel * AccumulatedLabel;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *withdrawRecordList;



@end

@implementation MSWithdrawalController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.withdrawRecordList = [NSMutableArray new];
    
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
    
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
    
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    //    self.view.backgroundColor = backgroundcolor;
    
    [self CreatView];
    
    
    [self tableView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}

- (void)ReloadData{
    [MBManager showLoading];
    NSDictionary * params = @{@"userId":[WYCAccountTool unarchiveuid],@"querytime":@"null",@"page":@(page),@"pageSize":@(pageSize)};
//    WYCLog(@"----------%@",params);
    [HTTPRequest POST:MSgetWithdrawRecord params:params succeed:^(id responseObject) {
//        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
                self.AccumulatedLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"withdrawSumMoney"]];
            count = [responseObject[@"data"][@"count"] integerValue];
            NSMutableArray *temporaryArray = [MSWithdrawalModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"withdrawRecordList"]];
            [temporaryArray addObjectsFromArray:self.withdrawRecordList];
            self.withdrawRecordList = temporaryArray;
//            self.withdrawRecordList = [MSWithdrawalModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"withdrawRecordList"]];
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)FootderRefreshReloadData{
    
    [MBManager showLoading];
    [self.withdrawRecordList removeAllObjects];
    NSDictionary * params = @{@"userId":[WYCAccountTool unarchiveuid],@"querytime":@"null",@"page":@(page),@"pageSize":@(pageSize)};
//    WYCLog(@"----------%@",params);
    [HTTPRequest POST:MSgetWithdrawRecord params:params succeed:^(id responseObject) {
//        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_footer endRefreshing];
            
            self.AccumulatedLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"withdrawSumMoney"]];
            self.withdrawRecordList = [MSWithdrawalModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"withdrawRecordList"]];
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page--;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)CreatView{
    UIView * HeaderView = [[UIView alloc] init];
    HeaderView.x = 0;
    HeaderView.y = 0;
    HeaderView.width = ScreenWidth;
    HeaderView.backgroundColor = backgroundcolor;
    
    
    
    /** 累计收入*/
    UILabel * AccumulatedTitle = [[UILabel alloc] initWithFrame:CGRectMake(52 * px, 30 * px, 200 * px, 60 * px)];
    AccumulatedTitle.textColor = [UIColor whiteColor];
    AccumulatedTitle.text = @"累计提现";
    AccumulatedTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [HeaderView addSubview:AccumulatedTitle];
    
    
    
    /** 累计收入*/
    UILabel * AccumulatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(AccumulatedTitle.x, CGRectGetMaxY(AccumulatedTitle.frame) + 20 * px, HeaderView.width * 0.5, AccumulatedTitle.height)];
    AccumulatedLabel.textColor = [UIColor whiteColor];
    AccumulatedLabel.text = @"33243.52元";
    AccumulatedLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(35 / 2)];
    [HeaderView addSubview:AccumulatedLabel];
    self.AccumulatedLabel = AccumulatedLabel;
    
    
    
    
//    UIView * timeView = [[UIView alloc] initWithFrame:CGRectMake(AccumulatedTitle.x, CGRectGetMaxY(AccumulatedLabel.frame) + 100 * px, ScreenWidth * 0.5, 100 * px)];
//    timeView.layer.cornerRadius = 10 * px;
//    timeView.layer.borderWidth = 1;
//    timeView.layer.borderColor = WYCRGBColor(80, 174, 159).CGColor;
//    [HeaderView addSubview:timeView];
//
//
//    /** 日历图标*/
//    UIButton * TimeButton = [[UIButton alloc] initWithFrame:CGRectMake(20 * px, 20 * px, 60 * px, 60 * px)];
//    //    [TimeButton setImage:[UIImage imageNamed:@"calendar icon"] forState:UIControlStateNormal];
//    [TimeButton setBackgroundImage:[UIImage imageNamed:@"calendar green"] forState:UIControlStateNormal];
//    TimeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
//    //    TimeButton.backgroundColor = [UIColor redColor];
//    [timeView addSubview:TimeButton];
//
//
//
//    /** 时间*/
//    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(TimeButton.frame) + 10 * px, TimeButton.y, timeView.width - TimeButton.width - 30 * px, TimeButton.height)];
//    timeLabel.textColor = [UIColor grayColor];
//    timeLabel.text = @"2019.03.26-2019.03.11";
//    timeLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
//    [timeView addSubview:timeLabel];
//
//
//    UIButton * ThreeMonthsButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeView.frame) + 40 * px, timeView.y, 180 * px, timeView.height)];
//    [ThreeMonthsButton setTitle:@"近三个月" forState:UIControlStateNormal];
//    [ThreeMonthsButton setTitleColor:BackViewColor forState:UIControlStateNormal];
//    ThreeMonthsButton.layer.cornerRadius = 10 * px;
//    ThreeMonthsButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
//    ThreeMonthsButton.layer.borderWidth = 1;
//    ThreeMonthsButton.layer.borderColor = BackViewColor.CGColor;
//    [HeaderView addSubview:ThreeMonthsButton];
//
//
//
//    UIButton * HalfYearButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(ThreeMonthsButton.frame) + 40 * px, timeView.y, 150 * px, timeView.height)];
//    [HalfYearButton setTitle:@"近半年" forState:UIControlStateNormal];
//    [HalfYearButton setTitleColor:BackViewColor forState:UIControlStateNormal];
//    HalfYearButton.layer.cornerRadius = 10 * px;
//    HalfYearButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
//    HalfYearButton.layer.borderWidth = 1;
//    HalfYearButton.layer.borderColor = BackViewColor.CGColor;
//    [HeaderView addSubview:HalfYearButton];
    
    
    UIView * TableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(AccumulatedLabel.frame) + 60 * px, HeaderView.width, 150 * px)];
    TableHeaderView.backgroundColor = [UIColor whiteColor];
    [HeaderView addSubview:TableHeaderView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:TableHeaderView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(30 * px, 30 * px)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = TableHeaderView.bounds;
    maskLayer.path = maskPath.CGPath;
    TableHeaderView.layer.mask = maskLayer;
    
    
    
    /** 收入明细图片*/
    UIImageView * EarningDetailedIMG = [[UIImageView alloc] initWithFrame:CGRectMake(52 * px, 45 * px, 60 * px, 60 * px)];
    EarningDetailedIMG.image = [UIImage imageNamed:@"txjl_icon"];
    [TableHeaderView addSubview:EarningDetailedIMG];
    
    
    /** 收入明细标题*/
    UILabel * EarningDetailedLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(EarningDetailedIMG.frame) + 20 * px, EarningDetailedIMG.y, 200 * px, EarningDetailedIMG.height)];
    EarningDetailedLabel.textColor = [UIColor blackColor];
    EarningDetailedLabel.text = @"提现明细";
    EarningDetailedLabel.textAlignment = NSTextAlignmentLeft;
    EarningDetailedLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    [TableHeaderView addSubview:EarningDetailedLabel];
    
    HeaderView.height = CGRectGetMaxY(TableHeaderView.frame);
    [self.view addSubview:HeaderView];
    
    self.tableView.tableHeaderView = HeaderView;
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        //        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.withdrawRecordList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSWithDrawalCell * cell = [MSWithDrawalCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor whiteColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.WithdrawalModel = self.withdrawRecordList[indexPath.row];
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"提现详情";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
